<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/21
 * Time: 03:45
 */

namespace app\api\controller\v1\product;
use app\api\controller\v1\Init;

class ProfileTemplate extends Init
{

    protected $model;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('product/profile_template');
    }


    public function item() {
        $result = $this->model
            ->where(['id' => $this->params['id']])->find();

        if($this->params['extra']) {
            $extra = (is_string($this->params['extra'])) ? explode(",", $this->params['extra']) : $this->params['extra'];
            array_map(function($e) use (&$result) {
                $e = trim($e);
                return $result->$e;
            }, $extra);
        }


        return $this->response('success', $result);
    }

    /**
     * 创建资料清单模板
     * @return \think\Response
     */
    public function create() {
        $model = model('product/profile_template', 'service');
        $result = $model->create($this->params['title'], $this->params['profile'], $this->params['product_id'], $this->user['id']);
        if(!$result) {
            return $this->response($model->getError(), [], -20002);
        }
        return $this->response('success', $result);
    }
}